import { createRouter, createWebHashHistory } from 'vue-router'
import { useUserStore } from '@/stores/index'

const router = createRouter({
    history: createWebHashHistory(), //createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'layout',
            redirect: '/iot/home',
            component: () => import('@/views/layout.vue'),
            meta: {
                requireAuth: true,
                keepAlive: true,
                id: 'layout',
            },
            children: [
                {
                    path: '/iot/home',
                    name: 'iotHome',
                    component: () => import('@/views/iot/home.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: true,
                        name: '首页',
                    },
                },
                {
                    path: '/iot/warn',
                    name: 'iotWarn',
                    component: () => import('@/views/iot/warn.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: true,
                        name: '告警中心',
                    },
                },
                {
                    path: '/iot/notice',
                    name: 'iotNotice',
                    component: () => import('@/views/iot/notice.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: true,
                        name: '通知管理',
                    },
                },
                {
                    path: '/iot/noticeDispose',
                    name: 'noticeDispose',
                    component: () => import('@/views/iot/noticeDispose.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: true,
                        parentName: '通知管理',
                        name: '通知配置',
                    },
                },
                {
                    path: '/iot/northbound/ali',
                    name: 'iotNorthbound',
                    component: () => import('@/views/iot/northbound.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: true,
                        parentName: '北向输出',
                        name: '阿里云',
                    },
                },
                {
                    path: '/iot/northbound/DataFeed',
                    name: 'DataFeed',
                    component: () => import('@/views/iot/DataFeed.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: true,
                        parentName: '北向输出',
                        name: '数据订阅',
                    },
                },
                {
                    path: '/iot/northbound/ali/new',
                    name: 'iotNorthboundNew',
                    component: () => import('@/views/iot/northboundNew.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: true,
                        grandParentName: '北向输出',
                        parentName: '阿里云',
                        name: '新建',
                    },
                },
                {
                    path: '/system/set',
                    name: 'system',
                    component: () => import('@/views/system/system.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '系统设置',
                    },
                },
                {
                    path: '/system/user',
                    name: 'systemUser',
                    component: () => import('@/views/system/user.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '用户设置',
                    },
                },
                {
                    path: '/system/network',
                    name: 'systemNetwork',
                    component: () => import('@/views/system/network.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '网络设置',
                    },
                },
                {
                    path: '/system/safe',
                    name: 'systemSafe',
                    component: () => import('@/views/system/safe.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '安全设置',
                    },
                },
                {
                    path: '/system/state',
                    name: 'systemState',
                    component: () => import('@/views/system/state.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '系统状态',
                    },
                },
                {
                    name: 'systemLog',
                    path: '/system/alarmLog',
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '日志管理',
                    },
                    children: [
                        {
                            path: '/system/alarmLog',
                            name: 'alarmLog',
                            component: () =>
                                import('@/views/system/alarmLog.vue'),
                            meta: {
                                requireAuth: true,
                                keepAlive: false,
                                name: '报警日志',
                            },
                        },
                    ],
                },
                {
                    path: '/system/driver',
                    name: 'systemSriver',
                    component: () => import('@/views/system/driver.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '驱动管理',
                    },
                },
                {
                    path: '/video/dashboard',
                    name: 'videoDashboard',
                    component: () => import('@/views/video/dashboard.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '仪表盘',
                    },
                },
                {
                    path: '/video/device',
                    name: 'videoDevice',
                    component: () => import('@/views/video/device.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '视频设备',
                    },
                },
                {
                    path: '/video/device/new',
                    name: 'videoDeviceNew',
                    component: () => import('@/views/video/deviceNew.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        parentName: '视频设备',
                        name: '新建',
                    },
                },
                {
                    path: '/video/device/channel',
                    name: 'videoDeviceChannel',
                    component: () => import('@/views/video/deviceChannel.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        parentName: '视频设备',
                        name: '通道列表',
                    },
                },
                {
                    path: '/video/device/singleVideo',
                    name: 'singleVideo',
                    component: () => import('@/views/video/videoPage.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        parentName: '视频设备',
                        name: '视频',
                    },
                },
                {
                    path: '/video/screen',
                    name: 'videoScreen',
                    component: () => import('@/views/video/screen.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '视频设备',
                    },
                },
                {
                    path: '/video/analysis',
                    name: 'videoAnalysis',
                    component: () => import('@/views/video/analysis.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '通道配置',
                        parentName: '视频分析',
                    },
                },
                {
                    path: '/video/analysisData',
                    name: 'analysisData',
                    component: () => import('@/views/video/analysisData.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '视频分析数据',
                        parentName: '视频分析',
                    },
                },
                {
                    path: '/video/cascade',
                    name: 'videoCascade',
                    component: () => import('@/views/video/cascade.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '国标级联',
                    },
                },
                {
                    path: '/video/cascade/new',
                    name: 'videoCascadeNew',
                    component: () => import('@/views/video/cascadeNew.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        parentName: '国标级联',
                        name: '新增',
                    },
                },
                {
                    path: '/fireControl/water',
                    name: 'fireControlWater',
                    component: () =>
                        import('@/views/fireControl/dashboard.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        parentName: '消防用水',
                        name: '仪表盘',
                    },
                },
                {
                    path: '/fireControl/water/device',
                    name: 'fireControlWaterDevice',
                    component: () => import('@/views/fireControl/device.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        parentName: '消防用水',
                        name: '设备管理',
                    },
                },
                {
                    path: '/fireControl/smoke',
                    name: 'fireControlSmoke',
                    component: () =>
                        import('@/views/fireControl/dashboard.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        parentName: '智慧烟感',
                        name: '仪表盘',
                    },
                },
                {
                    path: '/fireControl/smoke/device',
                    name: 'fireControlSmokeDevice',
                    component: () => import('@/views/fireControl/device.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        parentName: '智慧烟感',
                        name: '设备管理',
                    },
                },
                {
                    path: '/fireControl/fire',
                    name: 'fireControlFire',
                    component: () =>
                        import('@/views/fireControl/dashboard.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        parentName: '电气火灾',
                        name: '仪表盘',
                    },
                },
                {
                    path: '/fireControl/fire/device',
                    name: 'fireControlFireDevice',
                    component: () => import('@/views/fireControl/device.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        parentName: '电气火灾',
                        name: '设备管理',
                    },
                },
                {
                    path: '/fireControl/gas',
                    name: 'fireControlGas',
                    component: () =>
                        import('@/views/fireControl/dashboard.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        parentName: '可燃气体',
                        name: '仪表盘',
                    },
                },
                {
                    path: '/fireControl/gas/device',
                    name: 'fireControlGasDevice',
                    component: () => import('@/views/fireControl/device.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        parentName: '可燃气体',
                        name: '设备管理',
                    },
                },
                {
                    path: '/fireControl/Pump',
                    name: 'fireControlPump',
                    component: () =>
                        import('@/views/fireControl/dashboard.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        parentName: '消防水泵',
                        name: '仪表盘',
                    },
                },
                {
                    path: '/fireControl/Pump/device',
                    name: 'fireControlPumpDevice',
                    component: () => import('@/views/fireControl/device.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        parentName: '消防水泵',
                        name: '设备管理',
                    },
                },
                {
                    path: '/fireControl/transfer',
                    name: 'fireControlTransfer',
                    component: () =>
                        import('@/views/fireControl/dashboard.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        parentName: '用户传输',
                        name: '仪表盘',
                    },
                },
                {
                    path: '/fireControl/transfer/device',
                    name: 'fireControlTransferDevice',
                    component: () => import('@/views/fireControl/device.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        parentName: '用户传输',
                        name: '设备管理',
                    },
                },
                {
                    path: '/fireControl/:deviceClass',
                    name: 'fireControlDashborad',
                    component: () =>
                        import('@/views/fireControl/dashboard.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '仪表盘',
                    },
                },
                // {
                //     path: '/fireControl/device/:deviceClass',
                //     name: 'fireControlDevice',
                //     component: () => import('@/views/fireControl/device.vue'),
                //     meta: {
                //         requireAuth: true,
                //         keepAlive: false,
                //         name: '设备管理',
                //     },
                // },
                {
                    path: '/fireControl/equipment/:deviceClass',
                    name: 'fireControlEquipment',
                    redirect: '/fireControlEquipment',
                    // component: () => import('@/views/fireControl/equipment.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '设备管理',
                    },
                    children: [
                        {
                            path: '/fireControl/equipment/:deviceClass',
                            name: 'fireControlEquipment',
                            component: () =>
                                import('@/views/fireControl/equipment.vue'),
                            meta: {
                                requireAuth: true,
                                keepAlive: false,
                                name: '设备管理',
                            },
                        },
                        {
                            path: '/fireControl/device/:deviceClass',
                            name: 'fireControlDevice',
                            component: () =>
                                import('@/views/fireControl/device.vue'),
                            meta: {
                                requireAuth: true,
                                keepAlive: false,
                                parentName: '设备管理',
                                name: '设备列表',
                            },
                        },
                    ],
                },
                {
                    path: '/fireControl/alarm/:deviceClass',
                    name: 'fireControlAlarm',
                    component: () => import('@/views/fireControl/alarm.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '设备告警',
                    },
                },
                {
                    path: '/safe/carGate/plate',
                    name: 'carNumber',
                    component: () => import('@/views/safe/carGate/plate.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '车牌管理',
                    },
                },
                {
                    path: '/safe/carGate/plate/new',
                    name: 'carNumberNew',
                    component: () =>
                        import('@/views/safe/carGate/plateNew.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '车牌管理',
                    },
                },
                {
                    path: '/safe/carGate/device',
                    name: 'carGateDevice',
                    component: () => import('@/views/safe/carGate/device.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '设备管理',
                    },
                },
                {
                    path: '/safe/alam/dashboard/:deviceClass',
                    name: 'alamdashboard',
                    component: () => import('@/views/safe/alam/dashboard.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '仪表盘',
                    },
                },
                {
                    path: '/safe/alam/device/:deviceClass',
                    name: 'alarmdevice',
                    redirect: '/alarmdevice',
                    // component: () => import('@/views/safe/alam/device.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '设备管理',
                    },
                    children: [
                        {
                            path: '/safe/alam/device/:deviceClass',
                            name: 'alarmdevice',
                            component: () =>
                                import('@/views/safe/alam/device.vue'),
                            meta: {
                                requireAuth: true,
                                keepAlive: false,
                                name: '设备管理',
                            },
                        },
                        {
                            path: '/safe/alam/deviceList/:deviceClass',
                            name: 'alarmdeviceList',
                            component: () =>
                                import('@/views/safe/alam/list.vue'),
                            meta: {
                                requireAuth: true,
                                keepAlive: false,
                                name: '设备列表',
                            },
                        },
                    ],
                },
                {
                    path: '/safe/alam/deviceAlarm/:deviceClass',
                    name: 'deviceAlarm',
                    component: () => import('@/views/safe/alam/plateAlarm.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '设备告警',
                    },
                },
                {
                    path: '/safe/carGate/device/new',
                    name: 'carGateDeviceNew',
                    component: () =>
                        import('@/views/safe/carGate/deviceNew.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '设备管理',
                    },
                },
                {
                    path: '/energy/:deviceClass',
                    name: 'waterHome',
                    component: () => import('@/views/energy/home.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '仪表盘',
                    },
                },
                {
                    path: '/energy/device/:deviceClass',
                    name: 'energyDevice',
                    redirect: '/energyDevice',
                    // component: () => import('@/views/energy/device.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '设备管理',
                    },
                    children: [
                        {
                            path: '/energy/device/:deviceClass',
                            name: 'energyDevice',
                            component: () =>
                                import('@/views/energy/device.vue'),
                            meta: {
                                requireAuth: true,
                                keepAlive: false,
                                name: '设备管理',
                            },
                        },
                        {
                            path: '/energy/list/:deviceClass',
                            name: 'energyDeviceList',
                            component: () => import('@/views/energy/list.vue'),
                            meta: {
                                requireAuth: true,
                                keepAlive: false,
                                parentName: '设备管理',
                                name: '设备列表',
                            },
                        },
                    ],
                },
                {
                    path: '/energy/plateAlarm/:deviceClass',
                    name: 'waterDeviceAlarm',
                    component: () => import('@/views/energy/plateAlarm.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '设备告警',
                    },
                },
                {
                    path: '/svg/applicationList',
                    name: 'applicationList',
                    component: () => import('@/views/svg/applicationList.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '应用列表',
                    },
                },
                {
                    path: '/pluginmall/applicationList',
                    name: 'pluginmall',
                    component: () => import('@/views/pluginMall/index.vue'),
                    meta: {
                        requireAuth: true,
                        keepAlive: false,
                        name: '应用列表',
                    },
                },
            ],
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/views/login.vue'),
            meta: {
                requireAuth: false,
                keepAlive: false,
                preload: true,
            },
        },
        {
            path: '/realtimeAlarm',
            name: 'RealtimeAlarm',
            component: () => import('@/views/video/RealtimeAlarm/index.vue'),
            meta: {
                requireAuth: true,
                keepAlive: false,
            },
        },
        {
            path: '/edit',
            name: 'edit',
            component: () => import('@/views/edit.vue'),
            meta: {
                requireAuth: true,
                keepAlive: false,
            },
        },
        {
            path: '/preview',
            name: 'preview',
            component: () => import('@/views/preview.vue'),
            meta: {
                requireAuth: false,
                keepAlive: false,
            },
        },
    ],
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            return { top: 0 }
        }
    },
})

router.beforeEach((to, from, next) => {
    const userStore = useUserStore()
    if (to.meta.requireAuth) {
        // 判断该路由是否需要登录权限
        if (!!userStore.userInfo) {
            // 通过vuex state获取当前的loginStatus
            next()
        } else {
            next({
                path: '/login',
            })
        }
    } else {
        next()
    }
})

export default router
